package com.gpframework.module.controller.ctn;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.ctn.domain.GpItemTranslation;
import com.gpframework.module.ctn.domain.req.GpItemTranslationAdd;
import com.gpframework.module.ctn.domain.req.GpItemTranslationReq;
import com.gpframework.module.ctn.domain.req.GpItemTranslationUpdate;
import com.gpframework.module.ctn.domain.vo.GpItemTranslationExcel;
import com.gpframework.module.ctn.domain.vo.GpItemTranslationVo;
import com.gpframework.module.ctn.service.IGpItemTranslationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpItemTranslationController
 * @Author gpframework
 * @Date 2024-04-15
 * @Version 1.0.0
 * @Description 品名翻译表Controller
 */
@Validated
@Api(tags = "品名翻译表")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/ctn/itemtranslation" )
public class GpItemTranslationController extends BaseController {

    private final IGpItemTranslationService iGpItemTranslationService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-04-15
  * @Param gpItemTranslation
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('ctn:itemtranslation:list')")
    @GetMapping("/list")
    public TableDataInfo<GpItemTranslationVo> list(GpItemTranslationReq gpItemTranslationReq)
    {
        startPage();
        QueryWrapper<GpItemTranslation> lqw = new QueryWrapper<GpItemTranslation>();
        gpItemTranslationReq.generatorQuery(lqw,true);
        List<GpItemTranslationVo> list = iGpItemTranslationService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-04-15
     * @Param gpItemTranslation
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('ctn:itemtranslation:add')" )
    @Log(title = "添加品名翻译表" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpItemTranslationAdd gpItemTranslationAdd) {
        GpItemTranslation gpItemTranslation = Convert.convert(new TypeReference<GpItemTranslation>() {}, gpItemTranslationAdd);
        return toAjax(iGpItemTranslationService.save(gpItemTranslation) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-04-15
     * @Param gpItemTranslation
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('ctn:itemtranslation:edit')" )
    @Log(title = "修改品名翻译表" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpItemTranslationUpdate gpItemTranslationUpdate) {
        GpItemTranslation gpItemTranslation = Convert.convert(new TypeReference<GpItemTranslation>() {}, gpItemTranslationUpdate);
        return toAjax(iGpItemTranslationService.updateById(gpItemTranslation) ? 1 : 0);
    }


    /**
     * @Description 导出品名翻译表模板
     * @Author gpframework
     * @Date 2023-05-24
     * @Param gpCustomer
     * @Return TableDataInfo
     */
    @ApiOperation("导出品名翻译表模板")
    @Log(title = "导出品名翻译表模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult exportTemplate() {
        ExcelUtil<GpItemTranslationExcel> util = new ExcelUtil<>(GpItemTranslationExcel.class);
        return util.exportExcel(GpItemTranslationExcel.list(), "模板");
    }


    @ApiOperation("导入")
    @PreAuthorize("@ss.hasPermi('manufacture:weightReductionRuleSetting:add')" )
    @Log(title = "导入品名翻译表" , businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importList(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }


      // 导入新数据
        ExcelUtil<GpItemTranslationExcel> util = new ExcelUtil<GpItemTranslationExcel>(GpItemTranslationExcel. class);
        List<GpItemTranslationExcel> excelList = util.importCacheExcel(file.getInputStream());
        List<GpItemTranslation> list = Convert.convert(new TypeReference<List<GpItemTranslation>>() {}, excelList);

        iGpItemTranslationService.saveBatch(list);
        return AjaxResult.success("导入成功");
    }
    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-04-15
     * @Param gpItemTranslation
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('ctn:itemtranslation:export')" )
    @Log(title = "导出品名翻译表" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpItemTranslationReq gpItemTranslationReq) {
        QueryWrapper<GpItemTranslation> lqw = new QueryWrapper<GpItemTranslation>();
        gpItemTranslationReq.generatorQuery(lqw,true);
        List<GpItemTranslationVo> list = iGpItemTranslationService.listVo(lqw);
        ExcelUtil<GpItemTranslationVo> util = new ExcelUtil<GpItemTranslationVo>(GpItemTranslationVo. class);
        return util.exportExcel(list, "itemtranslation" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-04-15
     * @Param gpItemTranslation
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('ctn:itemtranslation:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpItemTranslationVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpItemTranslationService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-04-15
     * @Param gpItemTranslation
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('ctn:itemtranslation:remove')" )
    @Log(title = "删除品名翻译表" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpItemTranslationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
